package com.hafu.impl;

import com.hafu.dao.UserDao;
import com.hafu.model.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// 用户数据访问对象实现类
public class UserDAOImpl implements UserDao {
    private static final String URL = "jdbc:mysql://localhost:3306/education_system";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    private Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void closeResources(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User login(String username, String password) {
        User user = null;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        if (conn == null) {
            System.out.println("Database connection is not initialized.");
            return null;
        }

        try {
            String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username != null ? username.trim() : "");
            pstmt.setString(2, password != null ? password.trim() : "");

            System.out.println("Executing SQL: " + pstmt.toString());

            rs = pstmt.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getString("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));

                System.out.println("User found: " + user.getUsername());
            } else {
                System.out.println("No user found with the given credentials.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(rs, pstmt, conn);
        }

        return user;
    }

    @Override
    public boolean addUser(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            String sql = "INSERT INTO user (user_id, username, password, role) VALUES (?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getUserId().trim());
            pstmt.setString(2, user.getUsername().trim());
            pstmt.setString(3, user.getPassword().trim());
            pstmt.setString(4, user.getRole().trim());

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }
}
